home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / processes / launchwithdoc / launchwithdoc.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  5.6 KB  |  133 lines

  1. /*
  2.     File:        LaunchWithDoc.c
  3.  
  4.     Contains:    The smallest LaunchAplication example I could come up with.    
  5.                 this launches an application with a 'odoc' AppleEvent™    
  6.                 this little bit of code here uses two Standard File calls to get    
  7.                 the application to launch and the file to launch with, you will of course    
  8.                 want to replace these with whatever you want to do     
  9.                 ••• NOTE: This also works for launching non-System 7 applications.     
  10.                 If the Finder™ sees an 'odoc' or 'pdoc' Apple event in the launch block     
  11.                 and the application being launched is NOT system 7 aware, the Finder    
  12.                 coerces the Apple event into puppetstrings.  So you     
  13.                 don't need to special case for non-7.0 applications.      
  14.                 Pretty neat, huh?    
  15.                 ••• Important: I'm not doing any error checking, you of course should     
  16.                 so your users don't hunt you down and burn your keyboard.
  17.                 Please see the sample FinderOpenSel 1.0.1 for more information on 
  18.                 opening/launching applications and stuff.  Using the Finder event is much
  19.                 more versitile than just using launchApplication.
  20.  
  21.     Written by: C.K. Haun    
  22.  
  23.     Copyright:    Copyright © 1984-1999 by Apple Computer, Inc., All Rights Reserved.
  24.  
  25.                 You may incorporate this Apple sample source code into your program(s) without
  26.                 restriction. This Apple sample source code has been provided "AS IS" and the
  27.                 responsibility for its operation is yours. You are not permitted to redistribute
  28.                 this Apple sample source code as "Apple sample source code" after having made
  29.                 changes. If you're going to re-distribute the source, we require that you make
  30.                 it clear in the source that the code was descended from Apple sample source
  31.                 code, but that you've made changes.
  32.  
  33.     Change History (most recent first):
  34.                 7/27/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  35.                 
  36. */
  37. #include <Dialogs.h>
  38. #include <QuickDraw.h>
  39. #include <Windows.h>
  40. #include <Menus.h>
  41. #include <Fonts.h>
  42. #include <appleevents.h>
  43. #include <processes.h>
  44. #include <files.h>
  45. #include <StandardFile.h>
  46. #include <Aliases.h>
  47.  
  48. void main()
  49. {
  50.     FSSpec launchApp, fileToLaunch;   /* file spec for the app (launchApp) and the file (fileToLaunch) */
  51.     OSErr myErr;
  52.     LaunchParamBlockRec launchThis;
  53.     AEDesc myAddress;
  54.     ProcessSerialNumber myPSN;
  55.     AEDesc docDesc, launchDesc;
  56.     AEDescList theList;
  57.     AliasHandle withThis;
  58.     StandardFileReply myReply;
  59.     AppleEvent theEvent;
  60.     InitGraf((Ptr)&qd.thePort);  /* standard setup stuff */
  61.     InitFonts();
  62.     InitWindows();
  63.     InitMenus();
  64.     TEInit();
  65.     InitDialogs(nil);
  66.     InitCursor();
  67.     /* get the app to launch, using the Sys7 versions of Standard File */
  68.     /* which return FSSpecs */
  69.     /* Preset our address descriptor to our PSN */
  70.     /* The Finder is going to change this to whatever is appropriate  */
  71.     /* for the application that eventually gets launched, so I'll */
  72.     /* just fill it with my own address. */
  73.     /* I'm doing this because I have heard sporadic reports that  */
  74.     /* _not_ doing this causes address errors sometimes, I've never had that happen. */
  75.     /* But since it has, you should prefill. */
  76.     GetCurrentProcess(&myPSN);
  77.     /* create the address desc for the event */
  78.     myErr = AECreateDesc(typeProcessSerialNumber, (Ptr)&myPSN, sizeof(ProcessSerialNumber), &myAddress);
  79.     /* get the application to launch */
  80.     StandardGetFile(nil, -1, nil, &myReply);
  81.     if (!myReply.sfGood)
  82.         return;
  83.     launchApp = myReply.sfFile;
  84.     /* stuff it in my launch parameter block */
  85.     launchThis.launchAppSpec = &launchApp;
  86.     
  87.     /* get the file to pass */
  88.     StandardGetFile(nil, -1, nil, &myReply);
  89.     if (!myReply.sfGood)
  90.         return;
  91.     
  92.     /* the caller may have already done this, but it doesn't hurt to do it again */
  93.     fileToLaunch = myReply.sfFile;
  94.     /* create an appleevent to carry it */
  95.     AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, &myAddress, kAutoGenerateReturnID, kAnyTransactionID, &theEvent);
  96.     /* create a list for the alaises.  In this case, I only have one, but you still need */
  97.     /* a list */
  98.     AECreateList(nil, 0, false, &theList);
  99.     /* create an alias out of the file spec */
  100.     /* I'm not real sure why I did this, since there is a system coercion handler for */
  101.     /* alias to FSSpec, but I'm paranoid */
  102.     NewAlias(nil, &fileToLaunch, &withThis);
  103.     HLock((Handle)withThis);
  104.     /* now create an alias descriptor */
  105.     AECreateDesc(typeAlias, (Ptr)*withThis, GetHandleSize((Handle)withThis), &docDesc);
  106.  
  107.     HUnlock((Handle)withThis);
  108.     /* put it in the list */
  109.     AEPutDesc(&theList, 0, &docDesc);
  110.     AEPutParamDesc(&theEvent, keyDirectObject, &theList);
  111.     /* coerce the event from being an event into being appParms */
  112.     /* •••• If you just want to send an 'odoc' to an application that is */
  113.     /* already running, you can stop here and do an AESend on theEvent */
  114.     AECoerceDesc(&theEvent, typeAppParameters, &launchDesc);
  115.     HLock((Handle)theEvent.dataHandle);
  116.     /* and stuff it in the parameter block */
  117.     /* This is a little weird, since we're actually moving the event out of the */
  118.     /* AppParameters descriptor.  But it's necessary, the coercison to typeAppParameters */
  119.     /* stuffs the whole appleevent into one AERecord (instead of a AEDesc) so  */
  120.     /* the Finder gets the whole event as one handle.  It can then parse it itself */
  121.     /* to do the sending */
  122.     launchThis.launchAppParameters = (AppParametersPtr)*(launchDesc.dataHandle);
  123.     /* launch the thing */
  124.     launchThis.launchBlockID = extendedBlock;
  125.     launchThis.launchEPBLength = extendedBlockLen;
  126.     launchThis.launchFileFlags = nil;
  127.     launchThis.launchControlFlags = launchContinue + launchNoFileFlags;
  128.     LaunchApplication(&launchThis);
  129.     /* and launch it */
  130.     
  131.     
  132. }
  133.